package ua.com.infopulse.db;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * User: patronus
 */
public class DBHelper extends SQLiteOpenHelper {
    private String TAG = DBHelper.class.getSimpleName();
    private Context context;


    private static String db_name = "data_tracking";
    private static int db_version = 2;

    public static final String TABLE_TRACKING_DATA = "tracking_data";
    /**
     * int
     */
    private static final String ID = "_id";
    /*tracking_data table fields*/
    public static final String TABLE_TRACKING_DATA_ID = ID;
    /**
     * float
     */
    public static final String TABLE_TRACKING_DATA_DATE = "date";
    /**
     * float
     */
    public static final String TABLE_TRACKING_DATA_LATITUDE = "latitude";
    /**
     * float
     */
    public static final String TABLE_TRACKING_DATA_LONGITUDE = "longitude";
    /**
     * float
     */
    public static final String TABLE_TRACKING_DATA_ACCURACY = "accuracy";
    /**
     * text
     */
    public static final String TABLE_TRACKING_DATA_DESCRIPTION = "description";
    /**
     * 1 if true<br/>0 if false
     */
    public static final String TABLE_TRACKING_DATA_IS_ACCURACY_AVAILABLE = "accuracy_state";

    private static final String CREATE_TABLE_TRACKING_DATA =
            "create table " + TABLE_TRACKING_DATA +
                    " (" + ID + " integer primary key autoincrement, " +
                    TABLE_TRACKING_DATA_DATE + " real, " +
                    TABLE_TRACKING_DATA_IS_ACCURACY_AVAILABLE + " integer, " +
                    TABLE_TRACKING_DATA_ACCURACY + " real, " +
                    TABLE_TRACKING_DATA_LATITUDE + " real, " +
                    TABLE_TRACKING_DATA_LONGITUDE + " real, " +
                    TABLE_TRACKING_DATA_DESCRIPTION + " text);";

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public DBHelper(Context context) {
        super(context, db_name, null, db_version);
        Log.d(TAG, "Start creating");
        this.context = context;
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
        this.context=context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        Log.d(TAG, "DB onCreate");
        sqLiteDatabase.beginTransaction();
        try {
            sqLiteDatabase.execSQL(CREATE_TABLE_TRACKING_DATA);
            sqLiteDatabase.execSQL("PRAGMA case_sensitive_like = 0");
            sqLiteDatabase.setTransactionSuccessful();
        } catch (SQLException e) {
            Log.d(TAG, "can't create database ", e);
        } finally {
            sqLiteDatabase.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.beginTransaction();
        try {
            sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_TRACKING_DATA);
            sqLiteDatabase.setTransactionSuccessful();

        } catch (SQLException e) {
            Log.d(TAG, "can't delete database ", e);
        } finally {
            sqLiteDatabase.endTransaction();
        }
        onCreate(sqLiteDatabase);
    }
}
